package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.JOptionPane;
import javax.swing.JToggleButton;
import javax.swing.JScrollBar;
import javax.swing.LayoutStyle.ComponentPlacement;

import java.awt.TextArea;

import javax.swing.JTextArea;
import javax.swing.JComboBox;
import javax.swing.JTextPane;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JTextField;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.JCheckBox;

import controllers.ClientController;
import controllers.DBConnection;
import messages.ComplaintMessage;
import java.awt.event.ContainerAdapter;
import java.awt.event.ContainerEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.border.BevelBorder;
import java.awt.Color;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;
import javax.swing.ListSelectionModel;
import javax.swing.ImageIcon;

import entity.UserLoginInfo;
public class ReadComplaints extends JFrame {

	ClientController client;
	private JPanel contentPane;
	private JTextPane textField_2;
	private JTextPane textField;
	public String text2;
	public String cusID;
	public String mail;
	public String amount;
	private JTextField amoutfield;
	String selectedComplaintId;
	int ItemList;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ReadComplaints frame = new ReadComplaints();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the Read Complaints frame.
	 */
	public ReadComplaints() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 660, 480);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblReadComplaints = new JLabel("Read Complaints");
		lblReadComplaints.setBounds(214, 5, 191, 32);
		lblReadComplaints.setFont(new Font("Tahoma", Font.ITALIC, 20));
		
		JLabel lblSelectComplaint = new JLabel("(Complain ID)");
		lblSelectComplaint.setBounds(31, 64, 79, 14);
		
		final JList<String> list = new JList<String>();
		list.setBounds(31, 84, 83, 132);
		list.setVisibleRowCount(30);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setBorder(null);
		list.addComponentListener(new ComponentAdapter() {
			
			public void componentMoved(ComponentEvent arg0) {
			}
		});
		
		try
			{
			/**
			 * Reading the complaint text and show it on the GUI
			 */
				DefaultListModel<String> model = new DefaultListModel<String>();
				DBConnection conn1=new DBConnection();
				java.sql.PreparedStatement pr9= conn1.getDBConnection().prepareStatement("SELECT complaintID FROM sgi_system.complaints ");
				ResultSet rs9 = pr9.executeQuery();
			
				while(rs9.next()){
					this.ItemList=rs9.getInt("complaintID");
					model.addElement(String.valueOf(ItemList));
				}
				System.out.println(model);
				list.setModel(model);
				
				
				
			}//try
			catch (Exception e){e.printStackTrace();}
		
		
		
		list.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				
	
			}
		});
	
	

		
		JButton btnRead = new JButton("Read");
		btnRead.setBounds(15, 234, 122, 23);
		
		btnRead.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try{
					
				DBConnection conn1=new DBConnection();
				java.sql.PreparedStatement pr10= conn1.getDBConnection().prepareStatement("SELECT text,customerID,time FROM sgi_system.complaints WHERE complaintID=? ");
				pr10.setString(1, list.getSelectedValue());
				ResultSet rs10 = pr10.executeQuery();
				selectedComplaintId= list.getSelectedValue();
				while (rs10.next())
				{
					textField_2.setText("Complaint: "+rs10.getString("text")+"\nCustomer ID: "+rs10.getInt("customerID")+"\nComplaint recived at: "+rs10.getString("time"));
					cusID=rs10.getString("customerID");
				}
		
				
				
				}catch(Exception e){e.printStackTrace();}
					
				
			}
			
		});
		
		textField_2 = new JTextPane();
		textField_2.setBounds(161, 84, 231, 132);
		textField_2.setEditable(false);
		textField_2.setToolTipText("");
		
		JButton btnBack = new JButton("Back");
		btnBack.setBounds(264, 332, 128, 33);
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				WorkerGUI frame = new WorkerGUI();
				frame.setVisible(true);
				dispose();
			}
		});
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.setBounds(143, 332, 89, 32);
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		
		JLabel label = new JLabel("Response to the client :");
		label.setBounds(402, 64, 159, 14);
		
		JLabel lblComplainDetails = new JLabel("Complain details :");
		lblComplainDetails.setBounds(143, 64, 159, 14);
		
		 textField = new JTextPane();
		 textField.setBounds(398, 84, 231, 132);
		
		JLabel lblRefundAmount = new JLabel("Refund Amount :");
		lblRefundAmount.setBounds(357, 273, 103, 14);
		
		
		
		JButton button = new JButton("Post Reply");
		button.setBounds(410, 332, 136, 33);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					buttonActionPerformed(e);
					JOptionPane.showMessageDialog(null, "Message as been sent to the customer successfuly ");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		contentPane.setLayout(null);
		contentPane.add(lblReadComplaints);
		contentPane.add(btnRead);
		contentPane.add(lblSelectComplaint);
		contentPane.add(list);
		contentPane.add(lblComplainDetails);
		contentPane.add(label);
		contentPane.add(btnCancel);
		contentPane.add(btnBack);
		contentPane.add(textField_2);
		contentPane.add(textField);
		contentPane.add(lblRefundAmount);
		contentPane.add(button);
		
		amoutfield = new JTextField();
		amoutfield.setBounds(475, 270, 86, 20);
		contentPane.add(amoutfield);
		amoutfield.setColumns(10);
		
		JLabel lblSelectComplaint_1 = new JLabel("Select Complaint :");
		lblSelectComplaint_1.setBounds(31, 43, 89, 14);
		contentPane.add(lblSelectComplaint_1);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon("images//bla.jpg"));
		lblNewLabel.setBounds(0, 0, 644, 441);
		contentPane.add(lblNewLabel);
		lblNewLabel.setLayout(null);
	}
	
	public void buttonActionPerformed(ActionEvent e) throws IOException {
		ArrayList <String> array= new ArrayList <String>();
		text2=textField.getText();
		try{
			DBConnection conn2=new DBConnection();
		java.sql.PreparedStatement pr2= conn2.getDBConnection().prepareStatement("SELECT email FROM sgi_system.users WHERE ID=? ");
		pr2.setString(1, cusID);
		ResultSet rs2 = pr2.executeQuery();
		while(rs2.next()){
		mail=rs2.getString("email");
		
		}
		amount=amoutfield.getText();
		}
		catch (Exception e1){e1.printStackTrace();}
		UserLoginInfo logi=new UserLoginInfo();
		String IP=logi.getIP();
		int port=5555;
		array.add("ReplayToClient");
		array.add(amount);
		array.add(text2);
		array.add(cusID);
		array.add(mail);
		array.add(selectedComplaintId);
		
		this.client=new ClientController(IP, port);		
		client.handleMessageFromGui(array);
	}
	
}